﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RUC.Core.Entities;
using System.Reflection;
using System.Reflection.Emit;
using MyBasicLib.Reflection.FastReflection.Emit;
using MyBasicLib.Extensions;
using System.Threading;
using System.Linq.Expressions;
using MyBasicLib.Utilities;
using MyBasicLib.Data;
using MyBasicLib.Reflection;
using System.Data.Odbc;
using System.Data.SqlClient;
using MyBasicLib.Data.ORMLiteNew;
using MyBasicLib.Collections.Generic;
using System.Configuration;

namespace MyBasicLib.PerformanceTest
{

    class Program
    {
      public  static string connectionStrings = "MySqlConnection";

        static void Main(string[] args)
        {
       
            var tCount1 = 500  ;
           
            DataTest.Run(tCount1);

            Console.Read();
        }

        private static void TestTrans()
        {
            var db = IOC.DefaultContainer.Resolve<IDatabase>();

            Action act1 = () =>
            {
                using (var conn = db.CreateNewOpenConnection())
                {
                    for (var c = 0; c < 3; c++)
                    {
                        var cmd = conn.CreateCommand();
                        cmd.CommandText = "select count(*) from  dbo.App  with(nolock)";
                        var i = cmd.ExecuteScalar();
                    }
                }
            };
            Action act2 = () =>
            {
                using (var conn = db.CreateNewOpenConnection())
                {
                    var tran = conn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
                    for (var c = 0; c < 3; c++)
                    {
                        var cmd = conn.CreateCommand();
                        cmd.CommandText = "select count(*) from  dbo.App  with(nolock)";

                        cmd.Transaction = tran;
                        var i = cmd.ExecuteScalar();
                    }
                    tran.Commit();
                }
            };
            Action act3 = () =>
            {
                using (var trans = new System.Transactions.TransactionScope())
                {
                    for (var c = 0; c < 3; c++)
                    {
                        using (var conn = db.CreateNewOpenConnection())
                        {
                            var cmd = conn.CreateCommand();
                            cmd.CommandText = "select count(*) from  dbo.App  with(nolock)";
                            var i = cmd.ExecuteScalar();
                        }

                    }
                    trans.Complete();
                }
            };
            Action act4 = () =>
            {
                using (var dc1 = new DataContext())
                {
                    for (var c = 0; c < 3; c++)
                    {
                        var i = dc1.GetTable<App>().Count();
                    }
                }
            };
            act1();
            act2();
            act3();
            act4();
            var tCount = 100;
            MyBasicLib.Diagnostics.CodeTimer.Initialize();
            MyBasicLib.Diagnostics.CodeTimer.Time("NoTransaction", tCount, act1);
            MyBasicLib.Diagnostics.CodeTimer.Time("Transaction", tCount, act2);
            MyBasicLib.Diagnostics.CodeTimer.Time("TransactionScop", tCount, act3);
            MyBasicLib.Diagnostics.CodeTimer.Time("DataContext", tCount, act4);

            Console.Read();
        }
    }
}